﻿// new表格
function Tabs(tabStructure, tabsName, container) {
    this.tabStructure = tabStructure;
    this.tabsName = tabsName;
    this.ulName = tabsName + "_ul";
    this.container = container || $("body");
    this.activeIframe="";
    this.tabMap={};
    this.tabsObj;
    this.tabsCount = 0;
    this.init();
}
Tabs.prototype = {
    init: function () {
        var that = this;
        this.isClose = this.tabStructure.isClose;
        this.tabList = this.tabStructure.tabList;
        this.tabIndex = this.tabStructure.tabIndex || 1;
        this.reBuildUrl=this.tabStructure.reBuildUrl;
        this.isFullLoad=this.tabStructure.isFullLoad;
        this.switchFun=this.tabStructure.switchFun;
        this.titleMap={};
        $('<div class="tabs" id="' + this.tabsName + '"></div>').appendTo(that.container);
        this.create();
    },
    createTitle: function (tab) {
        var that = this;
        that.tabMap[tab["id"]]=tab;
        var close = "";
        if (that.isClose) {
            close = '<span class="ui-icon ui-icon-close"  style="   display:-moz-inline-stack;"  role="presentation">移除</span>';
        }
        return ('<li url="' + that.reUrl(tab.url) + '" index="' + that.tabsCount + '" extTitle="'+tab.title+'" linkPanel="'+tab.id+'" ><a id="'+tab.id+'_" href="#'+tab.id+'">' + tab.title + '</a>' + close + '</li>');
    },
    createContent: function (tab,index) {
        var that = this;
        var content = "";
        if (tab.content) {
            content = '<div class="tabContent" id="' + tabName + '">' + tab.content + '</div>';
        }
        if (tab.url) {
        	var url=that.reUrl(tab.url);
            var isLoad = "";
            var src="";
            if(that.isFullLoad){
            	 isLoad = "isLoad=true ";
     	         src = ' src= "' + url + '"';
            }else{
            	if((that.tabIndex-1)==index){
            		isLoad = "isLoad=true;";
            		src = ' src= "' + url + '"';
            	}
            }
            var iframeName=tab.id + "_Iframe" ;
            that.titleMap[tab["title"]]=iframeName;
            content = '<div class="tabContent" id="' + tab.id + '" ' + isLoad + ' extSrc="' + url+ '"><iframe ' + src + ' width="100%" height="100%"  style="border:0px; margin:0px;padding:0px;" id="' + iframeName + '" name="' + tab.content + '"></iframe></div>';
        }
        return content;
    },
    reUrl:function(url){
    	if(this.reBuildUrl){
    		return this.reBuildUrl(url);
    	}
    	return url;
    },
    create: function () {
        var that = this;
        if (that.tabList) {
            var tabTitles = ['<ul id="' + that.ulName + '">'];
            var contents = [];
            for (var i = 0; i < that.tabList.length; i++) {
                that.tabsCount++;
                var tab = that.tabList[i];
                tab["id"]=tab.id || (that.tabsName + that.tabsCount);
                tabTitles.push(that.createTitle(tab));
                contents.push(that.createContent(tab, i));
            }
            tabTitles.push("</ul>");
            $("#" + that.tabsName).html(tabTitles.join("") + contents.join(""));
            that.tabsObj = $("#" + that.tabsName).tabs({
            		activate: function( event, ui ) {
            		  var panel=ui.newTab[0];
            		  if(panel){
            			 var linkPanelId= $(panel).attr("linkPanel");
            			 var linkPanel=$("#"+linkPanelId);
            			 if(!linkPanel.attr("isLoad")){
            				 var iframe=linkPanel.find("iframe")[0];
            				 if(iframe){
            					that.activeIframe=iframe;
            					iframe.src=linkPanel.attr("extSrc");
            					linkPanel.attr("isLoad",true);
            				 }
            			 }
            			 if(that.switchFun){
            				 that.switchFun(that.tabMap[linkPanelId]);
            			 }
            		  }
            	  }
            });
            that.tabsObj.find(".ui-tabs-nav").sortable({
                axis: "x",
                stop: function () {
                    that.tabsObj.tabs("refresh");
                }
            });
            that.tabsObj.delegate("span.ui-icon-close", "click", function () {
                var panelId = $(this).closest("li").remove().attr("aria-controls");
                $("#" + panelId).remove();
                that.tabsObj.tabs("refresh");
            });
        }
    },
    add: function (tabPojo) {
        var that = this;
        var index = 0;
        var ifFind = false;
        /**
         * 激活当前标签
         * */
        $("#" + that.ulName).find("li").each(
            function () {
                index++;
                if ($(this).attr("url") == tabPojo.url) {
                    that.tabsObj.tabs("option", "active", index - 1);
                    ifFind = true;
                    return;
                }
            }
        );
        /**
         * 新增并且及活当前标签
         * **/
        if (!ifFind) {
            that.tabsCount++;
            tabPojo["id"]= tabPojo.id || (that.tabsName + that.tabsCount);
            $("#" + that.ulName).append(that.createTitle(tabPojo));
            $("#" + that.tabsName).append(that.createContent(tabPojo));
            that.tabsObj.tabs("refresh");
            that.tabsObj.tabs("option", "active", index);
            that.tabList.push(tabPojo);
        }
    },
    close: function (title) {
        var that = this;
        var doClose = function (panelId) {
            $("#" + panelId).remove();
            that.tabsObj.tabs("refresh");
            for(var i=0;i<that.tabList.length;i++){
            	var tab=that.tabList(i);
            	if(tab.id==panelId){
            		that.tabList.splice(i,1);
            	};
            }
        };
        if (title) {
            $("#" + that.ulName).find("li a").each(function () {
                if ($(this).text() == title) {
                    var panelId = $(this).parent("li").remove().attr("aria-controls");
                    doClose(panelId);
                }
            });
        }
        else {
            var panelId = $("#" + that.ulName).find(".ui-tabs-active").remove().attr("aria-controls");
            doClose(panelId);
        }
    },
    activeByIndex: function (index) {
        var a = $(".selector").tabs("option", "active");
    },
    getActiveIframe:function(){
    	var v_tabId = $(".ui-tabs-active a").attr("id");
    	var v_actIfrWind = $("#"+v_tabId+"Iframe")[0].contentWindow;
    	return v_actIfrWind;
    },
    getActiveObj:function(){
    	var v_tabId = $(".ui-tabs-active a").attr("id");
    	v_tabId = v_tabId.replace("_","");
    	return this.tabMap[v_tabId];
    },
    refreshByTitle: function (title) {
    	this.getTabWinByTitle(title).location.reload();
    },
    getTabWinByTitle:function(title){
    	if(title){
	    	var frm=document.getElementById(this.titleMap[title]);
	    	return frm.contentWindow;
    	};
    },
    changeTitleByIndex:function(index,newTitle){
    	var that=this;
    	var tabPojo=that.tabList[index];
    	if(tabPojo){
    		tabPojo["title"]=newTitle;
    		that.tabList[index]=tabPojo;
    		var tabName= tabPojo.id || (that.tabsName + that.tabsCount);
    		$("#"+tabName+"_").html(newTitle);
    	}
    },
    refreshByIndex:function(index){
    	var tabPojo=this.tabList[index];
    	this.refreshByTitle(tabPojo["title"]);
    }
};
(function ($) {
    $.fn.newTabs = function (tabStructure) {
        var tname = "tname" + (new Date()).getTime() + "tabs";
        return new Tabs(tabStructure, tname, $(this)[0]);
    };
})(jQuery);

